<?php


namespace app\privatearea\service;


use app\common\lib\AliOss;
use OSS\Core\OssException;
use think\Env;

class Upload
{
    protected $pathType = [
        1 => [// 头像
            'path' => 'uploads/avatar', // 目录
            'size' => 15728640,   // 大小 1024*1024*15=2097152  15M
            'ext' => 'jpg,jpeg,png', // 后缀
        ],
        2 => [// 用户照片
            'path' => 'private_area/user_pic', // 目录
            'size' => 15728640,   // 大小 1024*1024*15=2097152  15M
            'ext' => 'jpg,jpeg,png', // 后缀
        ],
        3 => [// 答案文件
            'path' => 'private_area/answer_file', // 目录
            'size' => 314572800,   // 大小 1024*1024*300=314572800  300M
            'ext' => 'doc,docx,pdf,mp4,avi,mov,rm,jpg,jpeg,png', // 后缀
        ],
        6 => [ // 发票信息
            'path' => 'upload/invoice', // 发票上传
            'size' => 4194304,   // 大小 1024*1024*4=4194304  4M
            'ext' => 'jpg,jpeg,png,pdf', // 后缀
        ],
    ];

    /**
     * @param $type 1=>档案头像 2=>用户照片 3=>占星师上传的文件
     * @param $fileKey
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function upload($type, $fileKey)
    {
        if (!isset($this->pathType[$type]))
        {
            return show(config('code.error'), '上传类型异常');
        }
        $path = $this->pathType[$type]['path'];

        $file = request()->file($fileKey);

        if (isset($file))
        {
            $config = ['size' => $this->pathType[$type]['size'], 'ext' => $this->pathType[$type]['ext']];
            // tp5 文件上传验证
            $info = $file->validate($config)->oss($path);
            if (!$info)
            {
                return show(config('code.error'), $file->getError());
            }
            // 获取文件名
            $filename = $info->get_filename();
            // 字符串方式上传图片
            $content = file_get_contents($info->get_tmpname());
            try
            {
                $aliOss = AliOss::getInstance();
                $aliOss->ossUpload($filename, $content);
                if (in_array($type, [2, 3, 6], true))
                {   // 设置文件私有化
                    $aliOss->setFileAuthPri($filename);
                }
            }
            catch (OssException $e)
            {
                return show(config('code.error'), $e->getMessage());
            }
        } else
        {
            return show(config('code.error'), '文件不存在');
        }

        $url = '';
        if (in_array($type, [2, 3, 6], true))
        {
            $url = $aliOss->getAuth($filename);
            $url = str_replace('http://zhunle.oss-cn-beijing.aliyuncs.com', 'https://oss.goddessxzns.com', $url);
        }
        if ($type === 6)
        {
            $url = Env::get('host.domain') . '/web/agreement/astrologer_app_invoice_view.html?pdf_url=' . base64_encode($url);
        }
        if ($type === 6)
        {
            $filename = '/' . $filename;
        }
        return show(config('code.success'), '上传成功', ['path' => $filename, 'url' => $url]);
    }
}
